package com.zhenyi.youqu.core;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.redis.Redis;
import com.youqu.common.model.YqGroup;
import com.youqu.common.model.YqOrder;
import com.youqu.common.model.YqOrderCart;
import com.youqu.common.model.YqOrderInfo;
import com.youqu.common.model.YqPaidProduct;
import com.youqu.common.model.YqProduct;
import com.youqu.common.model.YqUser;
import com.youqu.common.model.YqUserOrderGroup;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 处理订单已完成
 * 
 * @author cen
 *
 */
public class OrderHandler {

	private void updateUserOrderStatus(Long userId) {
		if (userId == null) {
			return;
		}
		YqUser user = YqUser.dao.findById(userId);
		user.updateOrderCount();
		user.update();
	}

	public void onSuccess(YqOrder order) {
		Long userId = order.getUserId();
		Integer type = order.getType();
		order.updateOrderStatus();
		order.updateOrderInfo();
		updateUserOrderStatus(userId);
		try {
			updateUserPaidProduct(order);
		} catch (Exception e) {
			LogKit.warn("保存购买副本错误" + order.getId(), e);
		}
		if (type.equals(0)) {

		} else if (type.equals(YqOrder.TYPE_PRODUCT)) {
			onPushProductBill(order);
		} else if (type.equals(YqOrder.TYPE_GROUP)) {
			onPushGroupBill(order);
		}
	}

	/**
	 * 团购支付成功
	 * 
	 * @param order
	 */
	private void onPushGroupBill(YqOrder order) {
		Long orderId = order.getId();
		Long userId = order.getUserId();
		String data = order.getData();
		// String productIdStr = data.replace("productId:", "");
		// Long productId = Long.parseLong(productIdStr);
		// YqGroup group = YqGroup.dao.findBy(userId, productId);
		// Long groupId = group.getId();
		// YqUserOrderGroup yqUserOrderGroup = new YqUserOrderGroup();
		// yqUserOrderGroup.setUserId(userId);
		// yqUserOrderGroup.setGroupId(groupId);
		// yqUserOrderGroup.setOrderId(orderId);
		// yqUserOrderGroup.setStatus(1);
		// yqUserOrderGroup.save();

		Long groupId = Long.parseLong(data.replace("groupId:", ""));
		YqGroup group = YqGroup.dao.findById(groupId);
		YqUserOrderGroup userOrderGroup = YqUserOrderGroup.dao.findById(groupId, userId);
		userOrderGroup.setOrderId(orderId);
		userOrderGroup.setStatus(order.getStatus());
		userOrderGroup.update();

		group.updateGroupUserCount();

		Long productId = group.getProductId();
		YqProduct product = YqProduct.dao.findById(productId);
		product.updatePaidUsers();
	}

	/**
	 * 商品支付成功
	 * 
	 * @param order
	 */
	private void onPushProductBill(YqOrder order) {
		YqOrderInfo orderInfo = YqOrderInfo.dao.findById(order.getId());
		Long productId = orderInfo.getProductId();
		YqProduct product = YqProduct.dao.findById(productId);
		product.updatePaidUsers();
		try {
			int count = Redis.use().get("YQ_" + order.getOutTradeNo());
			Long storage = product.getStorage() - count;
			if (storage <= 0) {
				storage = 0L;
			}
			product.setStorage(storage);
			product.update();
		} catch (Exception e) {
			LogKit.warn("update storage error", e);
		}
	}

	/**
	 * 保存购买副本
	 * 
	 * @param order
	 */
	private void updateUserPaidProduct(YqOrder order) {
		Long orderId = order.getId();
		Long userId = order.getUserId();
		YqOrderCart orderCart = YqOrderCart.dao.findById(orderId);
		if (orderCart == null) {
			return;
		}
		String json = orderCart.getContent();
		JSONArray array = JSONUtil.parseArray(json);
		for (Object object : array) {
			JSONObject jsonObject = new JSONObject(object);
			Long productId = jsonObject.getLong("productId");
			Integer count = jsonObject.getInt("count");
			String format = jsonObject.getStr("format");
			YqPaidProduct paidProduct = new YqPaidProduct();
			YqProduct product = YqProduct.dao.findById(productId);
			paidProduct.setUserId(userId);
			paidProduct.setOrderId(orderId);
			paidProduct.setProductId(productId);
			paidProduct.setProductTitle(product.getTitle());
			paidProduct.setFormat(format);
			paidProduct.setCover(product.getCover());
			paidProduct.setCount(count);
			paidProduct.setProductInfo(JsonKit.toJson(product));
			paidProduct.save();
		}
	}
}
